-- This is a cleanup script that drops all databases where the name is the same
-- as a collation.
-- © Erland Sommarskog 2024.
USE tempdb
go
DECLARE @cur CURSOR,
        @db  sysname,
        @sql nvarchar(MAX)

SET @cur = CURSOR STATIC FOR
   SELECT name
   FROM   sys.databases db
   WHERE  name = collation_name

OPEN @cur

WHILE 1 = 1
BEGIN 
   FETCH @cur INTO @db
   IF @@fetch_status <> 0
      BREAK

   PRINT 'Dropping database ' + @db

   SELECT @sql = 'ALTER DATABASE ' + quotename(@db) + 
                 ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE ' +
                 ' DROP DATABASE ' + quotename(@db)
   EXEC (@sql)
END

   
 